package com.example.x6.configurationmaintenance.Utils;

import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Vector;

public class LogOperationUtil extends SuCommand {
    private final String TAG = getClass().getSimpleName();
    private final String continualLogDir = "/sdcard/MC_CONFIG_LOG/continualLog/";
    private final String continualLogPackDir = "/sdcard/MC_CONFIG_LOG/continualLogPack/";
    private final String tempLogDir = "/sdcard/MC_CONFIG_LOG/tempLog/";
    private final String tempLogPackDir = "/sdcard/MC_CONFIG_LOG/tempLogPack/";

    public Vector<String> logBackup(String mainFilePath) {
        if (!logMainAlways(mainFilePath) || !logRadioAlways(mainFilePath)) {
            return null;
        }
        return getpids();
    }

    public boolean logMainFront(String mainFilePath) {
        return logMainSingle(mainFilePath);
    }

    public boolean logRadioFront(String radioFilePath) {
        return logRadioSingle(radioFilePath);
    }

    public boolean dmesgFrong(String dmesgFilePath) {
        return dmesg(dmesgFilePath);
    }

    public boolean killProcess(String PID) {
        return killLogProcess(PID);
    }

    public boolean isLogProcessRunning(String PID) {
        return isLogRunning(PID);
    }

    public String packTempLog() {
        return tempLogPack();
    }

    public String packContinualLog() {
        return continualLogPack();
    }

    public boolean clearContinualLog() {
        return continualLogClear();
    }

    public boolean clearTempLog() {
        return tempLogClear();
    }

    public boolean clearContinualLogPack() {
        return continualLogPackClear();
    }

    public boolean clearTempLogPack() {
        return tempLogPackClear();
    }

    private boolean continualLogClear() {
        return execRootCmdSilent("rm -rf /sdcard/MC_CONFIG_LOG/continualLog/*") == 0;
    }

    private boolean tempLogClear() {
        return execRootCmdSilent("rm -rf /sdcard/MC_CONFIG_LOG/tempLog/*") == 0;
    }

    private boolean continualLogPackClear() {
        return execRootCmdSilent("rm -rf /sdcard/MC_CONFIG_LOG/continualLogPack/*") == 0;
    }

    private boolean tempLogPackClear() {
        return execRootCmdSilent("rm -rf /sdcard/MC_CONFIG_LOG/tempLogPack/*") == 0;
    }

    private String continualLogPack() {
        String timeStup = new SimpleDateFormat("MMdd_HHmmss").format(Long.valueOf(System.currentTimeMillis()));
        if (execRootCmdSilent("busybox tar -cvf /sdcard/MC_CONFIG_LOG/continualLogPack/" + timeStup + "_continual_log_pack.tar.bz2 " + "/sdcard/MC_CONFIG_LOG/continualLog/") != 0) {
            return null;
        }
        return "/sdcard/MC_CONFIG_LOG/continualLogPack/" + timeStup + "_continual_log_pack.tar.bz2";
    }

    private String tempLogPack() {
        String timeStup = new SimpleDateFormat("MMdd_HHmmss").format(Long.valueOf(System.currentTimeMillis()));
        if (execRootCmdSilent("busybox tar -cvf /sdcard/MC_CONFIG_LOG/tempLogPack/" + timeStup + "_temp_log_pack.tar.bz2 " + "/sdcard/MC_CONFIG_LOG/tempLog/") != 0) {
            return null;
        }
        return "/sdcard/MC_CONFIG_LOG/tempLogPack/" + timeStup + "_log_pack.tar.bz2";
    }

    private boolean logMainAlways(String mainFilePath) {
        if (mainFilePath == null) {
            return false;
        }
        String cmd = "logcat -v time -f " + mainFilePath + "_main.log";
        Log.w(this.TAG, cmd);
        return backRun(cmd);
    }

    private boolean logRadioAlways(String radioFilePath) {
        if (radioFilePath == null) {
            return false;
        }
        String cmd = "logcat -b radio -v time -f " + radioFilePath + "_radio.log";
        Log.w(this.TAG, cmd);
        return backRun(cmd);
    }

    private boolean logMainSingle(String mainFilePath) {
        if (mainFilePath == null) {
            return false;
        }
        String cmd = "logcat -v time -d -f " + mainFilePath;
        Log.w(this.TAG, cmd);
        if (execRootCmdSilent(cmd) == 0) {
            return true;
        }
        return false;
    }

    private boolean logRadioSingle(String radioFilePath) {
        if (radioFilePath == null) {
            return false;
        }
        String cmd = "logcat -b radio -v time -d -f " + radioFilePath;
        Log.w(this.TAG, cmd);
        if (execRootCmdSilent(cmd) == 0) {
            return true;
        }
        return false;
    }

    private boolean dmesg(String dmesgFilePath) {
        if (dmesgFilePath == null) {
            return false;
        }
        String cmd = "dmesg > " + dmesgFilePath + "_dmesg.log";
        Log.w(this.TAG, cmd);
        if (execRootCmdSilent(cmd) == 0) {
            return true;
        }
        return false;
    }

    public Vector<String> getpids() {
        Vector<String> pids = new Vector<>();
        Vector<String> result = execRootCmd("ps | grep logcat");
        if (result == null || result.size() == 0) {
            return null;
        }
        Iterator<String> it = result.iterator();
        while (it.hasNext()) {
            String[] tempString = it.next().split("\\s+");
            if (tempString == null || tempString.length < 3) {
                return null;
            }
            if (tempString[2].trim().equals("1")) {
                pids.add(tempString[1].trim());
            }
        }
        return pids;
    }

    private boolean backRun(String cmd) {
        if (cmd == null || cmd.isEmpty()) {
            return false;
        }
        if (execRootCmdSilent(cmd + " &") == 0) {
            return true;
        }
        return false;
    }

    private boolean killLogProcess(String PID) {
        if (PID == null) {
            return false;
        }
        if (execRootCmdSilent("kill " + PID) == 0) {
            return true;
        }
        return false;
    }

    private boolean isLogRunning(String PID) {
        Vector<String> results = execRootCmd("ps | grep \"logcat\"");
        if (results.size() == 0) {
            return false;
        }
        Iterator<String> it = results.iterator();
        while (it.hasNext()) {
            String[] temp = it.next().split("\\s+");
            if (temp.length >= 2 && temp[1].equals(PID)) {
                return true;
            }
        }
        return false;
    }

    @Override // com.example.x6.configurationmaintenance.Utils.SuCommand
    public int execRootCmdSilent(String cmd) {
        return super.execRootCmdSilent(cmd);
    }

    @Override // com.example.x6.configurationmaintenance.Utils.SuCommand
    public Vector<String> execRootCmd(String cmd) {
        return super.execRootCmd(cmd);
    }
}
